package com.sxkiler.demo.test.elasticsearch;

import org.frameworkset.elasticsearch.ElasticSearchHelper;
import org.frameworkset.elasticsearch.boot.ElasticSearchBoot;
import org.frameworkset.elasticsearch.client.ClientInterface;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import java.util.Map;

public class BaseESStarter {
    private BaseESProperties properties;
    private static final Logger log = LoggerFactory.getLogger(BaseESStarter.class);
    private ClientInterface restClient;

    public BaseESStarter() {
    }

    public BaseESStarter(BaseESProperties properties) {
        this.properties = properties;
    }
    public void start() {
            if (this.properties.getElasticsearch() != null) {
                Map ps = this.properties.buildProperties();
                if (ps != null && ps.size() > 0) {
                    ElasticSearchBoot.boot(ps, true);
                } else {
                    log.info("BBoss Elasticsearch Rest Client properties is not configed in spring application.properties file.Ignore load bboss elasticsearch rest client through spring boot starter.");
                }
            }
    }

    public ClientInterface getRestClient() {
        if (this.restClient == null) {
            synchronized(this) {
                if (this.restClient == null) {
                    this.restClient = ElasticSearchHelper.getRestClientUtil();
                }
            }
        }
        return this.restClient;
    }

    public ClientInterface getConfigRestClient(String elasticsearchName, String configFile) {
        return ElasticSearchHelper.getConfigRestClientUtil(elasticsearchName, configFile);
    }

    public ClientInterface getRestClient(String elasticsearchName) {
        return ElasticSearchHelper.getRestClientUtil(elasticsearchName);
    }

    public ClientInterface getConfigRestClient(String configFile) {
        return ElasticSearchHelper.getConfigRestClientUtil(configFile);
    }

    public BaseESProperties getProperties() {
        return properties;
    }

    public void setProperties(BaseESProperties properties) {
        this.properties = properties;
    }

    public String getHttpHostNames(){
        return "http://"+ properties.getElasticsearch().getRest().getHostNames();
    }
}
